package com.sanji.pay.allinpay.service;

import com.alibaba.fastjson.JSON;
import com.sanji.pay.allinpay.entity.AllinPay;
import com.sanji.pay.allinpay.util.SybPayService;
import com.sanji.pay.common.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.TimerTask;

/**
 * 定时查询
 * Created by cbh on 2017/8/26.
 */
public class AllinPayTaskService extends TimerTask {
  protected final Logger logger = LoggerFactory.getLogger(this.getClass());

  private String payNum;
  private String trxId;

  private SybPayService sybPayService;
  private AllinPayService allinPayService;

  public AllinPayTaskService(String payNum, String trxId) {
    this.payNum = payNum;
    this.trxId = trxId;
  }

  public AllinPayTaskService() {
  }

  @Override
  public void run() {
    try {
      Map<String,String> queryMap = sybPayService.query(payNum,trxId);
      logger.info("==========定时任务执行 queryMap:=========");
      logger.info(JSON.toJSONString(queryMap));
      if(!queryMap.isEmpty() && "0000".equals(queryMap.get("trxstatus"))){
        AllinPay allinPay = allinPayService.findByPayNum(queryMap.get("reqsn"));//商户订单号
        allinPay = allinPay==null? new AllinPay() : allinPay;
        allinPay.setTrxStatus(queryMap.get("trxstatus"));
        allinPay.setPayTime(DateUtil.getDateStrToDate(queryMap.get("fintime"),"yyyyMMddHHmmss"));
        allinPay.setTrxAmt(Integer.valueOf(queryMap.get("trxamt")));
        allinPayService.save(allinPay);
      }else{
        logger.info("===========定时任务完成:订单未支付============");
      }
    }catch (Exception e){
      e.printStackTrace();
      logger.info("查询订单失败==========" + payNum);
    }
  }

  public String getPayNum() {
    return payNum;
  }

  public void setPayNum(String payNum) {
    this.payNum = payNum;
  }

  public String getTrxId() {
    return trxId;
  }

  public void setTrxId(String trxId) {
    this.trxId = trxId;
  }

  public SybPayService getSybPayService() {
    return sybPayService;
  }

  public void setSybPayService(SybPayService sybPayService) {
    this.sybPayService = sybPayService;
  }

  public AllinPayService getAllinPayService() {
    return allinPayService;
  }

  public void setAllinPayService(AllinPayService allinPayService) {
    this.allinPayService = allinPayService;
  }
}
